package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @param type
     * @param page
     * @param pagesize
     * @param nickname
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity likeEachOther(@PathVariable Integer type,
                                        @RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        String nickname) {
        PageResult pageResult = userInfoService.likeEachOther(type, page, pagesize, nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 粉丝喜欢
     * @param uid
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable Long uid) {
        userInfoService.fans(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消喜欢
     * @param uid
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity removeLike(@PathVariable Long uid) {
        userInfoService.removeLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     *  修改手机号- 1 发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity updateMobile(){
        userService.updateMobile();
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        Map<String ,Boolean> rMap = userService.checkVerificationCode(map);
        return ResponseEntity.ok(rMap);
    }

    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){
        userService.updatePhone(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map result = userService.counts();
        return ResponseEntity.ok(result);
    }

}
